package cn.xiaobai.config;

import cn.xiaobai.domain.KeyAdapterModel;
import cn.xiaobai.listener.GlobalKeyListener;
import cn.xiaobai.service.HotKeyCustomService;
import cn.xiaobai.service.impl.HotKeyCustomServiceImpl;
import com.github.kwhat.jnativehook.GlobalScreen;
import com.github.kwhat.jnativehook.NativeHookException;
import com.github.kwhat.jnativehook.example.NativeHookDemo;
import com.github.kwhat.jnativehook.keyboard.NativeKeyEvent;
import com.github.kwhat.jnativehook.keyboard.NativeKeyListener;
import com.github.kwhat.jnativehook.keyboard.SwingKeyAdapter;
import com.melloware.jintellitype.JIntellitype;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.awt.*;

/**
 * @Author yangdaji
 * @Date 2022/10/12 0012
 * @PackageName com.supconit.suptap.gateway.xiaobai.conf
 * @ClassName ListenerConfig
 */
@Slf4j
@Configuration
public class ListenerConfig {

    @Bean
    public GlobalKeyListener globalKeyListener(@Qualifier("robot") Robot robot,
                                               @Qualifier("jIntellitypeInstance") JIntellitype jIntellitypeInstance,
                                               @Qualifier("hotKeyCustomService") HotKeyCustomService hotKeyCustomService,
                                               @Qualifier("keyAdapterModel") KeyAdapterModel keyAdapterModel
                                               ) {
        return new GlobalKeyListener(robot, jIntellitypeInstance, hotKeyCustomService,keyAdapterModel);
    }

    @Bean("robot")
    public Robot robot() {
        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            log.error("创建对象失败: {}", e.getMessage());
        }
        return robot;
    }

    @Bean("jIntellitypeInstance")
    public JIntellitype jIntellitypeInstance() {
        return JIntellitype.getInstance();
    }

    @Bean("hotKeyCustomService")
    public HotKeyCustomService hotKeyCustomService() {
        return new HotKeyCustomServiceImpl();
    }

    /**
     * 键盘适配
     */
    @Bean("keyAdapterModel")
    public KeyAdapterModel keyAdapterModel() {
        return new KeyAdapterModel();
    }


    public static void main(String[] args) throws NativeHookException {
        NativeKeyListener demo=new NativeKeyListener(){
            @Override
            public void nativeKeyTyped(NativeKeyEvent e) {
                NativeKeyListener.super.nativeKeyTyped(e);
                System.out.println("input key: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
                if (e.getKeyCode() == NativeKeyEvent.VC_ESCAPE) {
                    try {
                        GlobalScreen.unregisterNativeHook();
                    } catch (NativeHookException nativeHookException) {
                        nativeHookException.printStackTrace();
                    }
                }
            }

            @Override
            public void nativeKeyPressed(NativeKeyEvent nativeEvent) {
                NativeKeyListener.super.nativeKeyPressed(nativeEvent);
                System.out.println("Press the key: " + NativeKeyEvent.getKeyText(nativeEvent.getKeyCode()));
            }

            @Override
            public void nativeKeyReleased(NativeKeyEvent nativeEvent) {
                NativeKeyListener.super.nativeKeyReleased(nativeEvent);
                System.out.println("release: " + NativeKeyEvent.getKeyText(nativeEvent.getKeyCode()));
            }
        };
//        GlobalScreen.registerNativeHook();
//        GlobalScreen.addNativeKeyListener(demo);

//        String mapLibraryName = System.mapLibraryName("com\\melloware\\jintellitype\\windows\\JNativeHook");
//        System.out.println(mapLibraryName);
        System.loadLibrary("JIntellitype64");
//        System.out.println(System.getProperty("java.library.path"));
    }
}
